package com.dotmedia.dboperations;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.dotmedia.core.DatabaseAccessService;
import com.dotmedia.entities.UITranslations;

/**
 * This class will have the database operations for UITranslations
 * 
 * @author siddharthsaha
 * 
 */
public class UITranslationsDatabaseOperations {

	private static Logger logger = Logger.getLogger("UITranslationsDatabaseOperations");

	/**
	 * This method will persist an entity of UITranslations
	 * 
	 * @param uitranslationsEntity
	 *        The UITranslation entity which needs to be persisted
	 */
	public void persistUITranslationEntity(UITranslations uitranslationsEntity) {
		logger.info("IN method persistUITranslationEntity in UITranslationsDatabaseOperations");
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		int persitStatus = databaseAccessService.persistEntity(uitranslationsEntity);
		if (persitStatus == 0) {
			logger.info("Persisiting of UITranslations entity was successful. Id: " + uitranslationsEntity.getId());
		} else {
			logger.error("Persisting of UITranslations entity was not successful. Id: " + uitranslationsEntity.getId());
		}
		logger.info("OUT method persisitUITranslationEntity in UITranslationsDatabaseOperations");
	}

	/**
	 * This method will remove a UITranslation entity from the database.
	 * 
	 * @param uiTranslationsEntity
	 *        The ui translation entity which needs to be removed from the database
	 */
	public void removeUITranslationEntity(UITranslations uiTranslationsEntity) {
		logger.info("IN method removeUITranslationEntity() in UITranslationsDatabaseOperations");
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		int removeStatus = databaseAccessService.removeEntity(uiTranslationsEntity, uiTranslationsEntity.getId());
		if (removeStatus == 0) {
			logger.info("The UITranslation entity was successfully removed. Id: " + uiTranslationsEntity.getId());
		} else {
			logger.error("The UITranslation entity was not successfully removed. Id: " + uiTranslationsEntity.getId());
		}
		logger.info("OUT method removeUITranslationsEntity() in UITranslationsDatabaseOperations");
	}

	/**
	 * This method updates a UITranslation entity in the database.
	 * 
	 * @param uiTranslationsEntity
	 *        The UITranslation entity which needs to be updated
	 */
	public void updateUITranslationEntity(UITranslations uiTranslationsEntity) {
		logger.info("IN method updateUITranslationEntity() in UITranslationsDatabaseOperations");
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		String query = "SELECT uitranslationEntity FROM UITranslations uiTranslationEntity WHERE uiTranslationEntity.id = "
				+ uiTranslationsEntity.getId();
		logger.info("Query: " + query);
		List retrievedEntities = databaseAccessService.getEntities(query);
		boolean obtained = false;
		for (int retreivedEntitiesIndex = 0; retreivedEntitiesIndex < retrievedEntities.size(); retreivedEntitiesIndex++) {
			UITranslations currentEntity = (UITranslations) retrievedEntities.get(retreivedEntitiesIndex);
			if (currentEntity.getId().intValue() == uiTranslationsEntity.getId().intValue()) {
				obtained = true;
				logger.info("Obtained the UITranslation Entity which needs to be updated");
				currentEntity.setLangCode(uiTranslationsEntity.getLangCode());
				currentEntity.setDescription(uiTranslationsEntity.getDescription());
				int updateStatus = databaseAccessService.updateEntity(currentEntity);
				if (updateStatus == 0) {
					logger.info("The entity of UITranslations was successfully updated. Id: " + currentEntity.getId());
				} else {
					logger.error("The entity of UITranslations was not successfully updated. Id: " + currentEntity.getId());
				}
				break;
			}
		}
		if (!obtained) {
			logger.warn("The UITranslation to be updated was not found");
		}
		logger.info("OUT method updateUITranslationEntity() in UITranslationsDatabaseOperations");
	}

	/**
	 * This method returns a list of UITranslations entities with a specific lang code
	 * 
	 * @param langCode
	 * @return
	 */
	public List<UITranslations> getUITranslationsEntitiesWithLangCode(String langCode) {
		logger.info("IN method getUITranslationsEntitiesWithLangCode() in UITranslationsDatabaseOperations");
		List<UITranslations> toReturnEntities = new ArrayList<UITranslations>();
		String query = "SELECT uiTranslationEntity FROM UITranslations uiTranslationEntity WHERE uiTranslationEntity.langCode = '"
				+ langCode + "'";
		logger.info("Query: " + query);
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		List retrievedEntities = databaseAccessService.getEntities(query);
		for (int retrievedEntitiesIndex = 0; retrievedEntitiesIndex < retrievedEntities.size(); retrievedEntitiesIndex++) {
			UITranslations currentEntity = (UITranslations) retrievedEntities.get(retrievedEntitiesIndex);
			if (currentEntity.getLangCode().equals(langCode)) {
				// this is to be added to the return list
				toReturnEntities.add(currentEntity);
			}
		}
		logger.info("OUT method getUITranslationsEntitiesWithLangCode() in UITranslationsDatabaseOperations");
		return toReturnEntities;
	}

	public List<UITranslations> getAllUITranslationEntities() {
		logger.info("IN method getAllUITranslationEntities() in UITranslationsDatabaseOperations");
		List<UITranslations> listToBeReturned = new ArrayList<UITranslations>();
		String query = "SELECT uiTranslationEntity FROM UITranslations uiTranslationEntity";
		logger.info("Query: " + query);
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		List retrievedEntities = databaseAccessService.getEntities(query);
		for (int index = 0; index < retrievedEntities.size(); index++) {
			UITranslations currenEntity = (UITranslations) retrievedEntities.get(index);
			listToBeReturned.add(currenEntity);
		}
		logger.info("OUT method getAllUITranslationEntities() in UITranslationsDatabaseOperations");
		return listToBeReturned;
	}
}
